<?php

namespace App\Admin\Widgets;

use Illuminate\Support\Arr;

class SuperModal extends \Dcat\Admin\Widgets\Modal
{
    private $signedEvent = [];

    protected function addScript()
    {
        if (!$this->events) {
            return;
        }
        $id = $this->id();
        if (in_array($id, $this->signedEvent))
            return;
        array_push($this->signedEvent, $id);

        $script = '';

        foreach ($this->events as $v) {
            $script .= "console.log('$id' , 'sign event' ,'{$v['event']}' );target.off('{$v['event']}').on('{$v['event']}', function (event) {
                {$v['script']}
            });";
        }

        $this->script = <<<JS
(function () {
    console.log('$id' , 'init event');
    var target = $('#{$id}'), body = target.find('.modal-body');
    {$this->getRenderableScript()}
    {$script}
})();
JS;
    }
}
